/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.inspur.edp.cef.generator.apiGenerator.dataType.entityInterface.resourceInfo.initcolumnsstragety;

import com.inspur.edp.bef.bizentity.GspBizEntityElement;
import com.inspur.edp.caf.generator.baseInfo.TypeInfo;
import com.inspur.edp.caf.generator.utils.ExpressionUtils;
import com.inspur.edp.cef.designtime.api.IGspCommonDataType;
import com.inspur.edp.cef.designtime.api.IGspCommonField;
import com.inspur.edp.cef.generator.apiGenerator.dataType.entityInterface.DataTypeInterfaceGeneratorContext;
import com.inspur.edp.cef.generator.apiGenerator.dataType.entityInterface.resourceInfo.InitColumnsGenerator;
import com.inspur.edp.cef.spi.entity.info.propertyinfo.ComplexUdtPropertyInfo;
import com.inspur.edp.udt.designtime.api.entity.ComplexDataTypeDef;
import com.inspur.edp.udt.designtime.api.entity.UnifiedDataTypeDef;
import com.inspur.edp.udt.designtime.api.entity.dbInfo.ColumnMapType;
import lombok.var;
import org.eclipse.jdt.core.dom.AST;
import org.eclipse.jdt.core.dom.Block;
import org.eclipse.jdt.core.dom.BooleanLiteral;
import org.eclipse.jdt.core.dom.ClassInstanceCreation;
import org.eclipse.jdt.core.dom.MethodInvocation;
import org.eclipse.jdt.core.dom.Statement;
import org.eclipse.jdt.core.dom.StringLiteral;
import org.eclipse.jdt.core.dom.VariableDeclarationFragment;
import org.eclipse.jdt.core.dom.VariableDeclarationStatement;

public class AddComplexUdtPropertyStragety extends AddUdtPropertyInfoStragety {

  public AddComplexUdtPropertyStragety(Block block,
      IGspCommonDataType commonDataType,
      IGspCommonField field, AST ast,
      DataTypeInterfaceGeneratorContext parentContext,
      ComplexDataTypeDef unifiedDataTypeDef,
      ExtendAddPropertyInfoProcessor extendAddPropertyInfoProcessor) {
    super(block, commonDataType, field, ast, parentContext, unifiedDataTypeDef,
        extendAddPropertyInfoProcessor);
  }

  private ComplexDataTypeDef getUdt()
  {return (ComplexDataTypeDef) super.unifiedDataTypeDef;}

  @Override
  protected boolean addExtendPropertyArgs() {
    if(field instanceof GspBizEntityElement)
      return true;
    return false;
  }

  @Override
  protected void beforeAddMethodInvocation() {
    block.statements().add(createUdtInfoStatement(getUdt(), "objectType" + field.getLabelID(),
        ComplexUdtPropertyInfo.class));
    if(field instanceof GspBizEntityElement) {
      if (extendAddPropertyInfoProcessor.generateChildElemnts()) {
        for (var childElement : field.getChildElements()) {
          MethodInvocation invocation = ExpressionUtils
              .getMethodInvocation(ast, "objectType" + field.getLabelID(), "addPropertyInfo");
          block.statements().add(ast.newExpressionStatement(invocation));

          StringLiteral stringLiteral = ast.newStringLiteral();
          stringLiteral.setLiteralValue(childElement.getLabelID());
          invocation.arguments().add(stringLiteral);

          stringLiteral = ast.newStringLiteral();
          if (childElement.getI18nResourceInfoPrefix() == null || childElement
              .getI18nResourceInfoPrefix().equals(""))
            stringLiteral.setLiteralValue("");
          else
            stringLiteral.setLiteralValue(childElement.getI18nResourceInfoPrefix() + ".Name");
          invocation.arguments().add(stringLiteral);

          IGspCommonField refField = getUdt()
              .findElement(field.getMappingRelation().getMappingInfo(childElement.getID()));
          stringLiteral = ast.newStringLiteral();
          if (field.getMappingRelation().getMappingInfo(childElement.getID()) != null && field
              .getMappingRelation().getMappingInfo(childElement.getID()).equals(getUdt().getId()))
            stringLiteral.setLiteralValue(getUdt().getCode());
          else if (refField == null)
            stringLiteral.setLiteralValue(getUdt().getCode());
          else
            stringLiteral.setLiteralValue(refField.getLabelID());
          invocation.arguments().add(stringLiteral);

          extendAddPropertyInfoProcessor
              .addComplexExtendPropertyArgs(invocation.arguments(), childElement, commonDataType,
                  parentContext,
                  ast);
        }
      }
    }
  }

  @Override
  protected void addComplexAddStatement(MethodInvocation methodInvocation) {
    addDisplayValueKeyStatement(methodInvocation);
    methodInvocation.arguments().add(ast.newSimpleName("objectType" + field.getLabelID()));
  }

  @Override
  protected void addSimpleAddStatement(MethodInvocation methodInvocation) {
    methodInvocation.arguments().add(ast.newSimpleName("objectType" + field.getLabelID()));
  }

  @Override
  protected boolean useSimpleAddStatment() {
    if(isI18nPrefixEquals()==false)
      return  false;
    return true;
  }

  private Statement createUdtInfoStatement(UnifiedDataTypeDef udtDef, String varName, Class type) {
    TypeInfo info = new TypeInfo(type);
    StringLiteral stringLiteral = ast.newStringLiteral();
    stringLiteral.setLiteralValue(udtDef.getUdtType());
    BooleanLiteral booleanLiteral = ast.newBooleanLiteral(parentContext.getApiGenContext().getCefGenContext().getEnableStdDateTime());

    VariableDeclarationFragment fragment = ast.newVariableDeclarationFragment();
    fragment.setName(ast.newSimpleName(varName));
    ClassInstanceCreation creation = ast.newClassInstanceCreation();
    creation.setType(info.getType(ast, parentContext.getApiGenContext()));
    creation.arguments().add(stringLiteral);
    creation.arguments().add(booleanLiteral);
    fragment.setInitializer(creation);
    VariableDeclarationStatement statement = ast.newVariableDeclarationStatement(fragment);

    statement.setType(
        info.getType(ast, parentContext.getApiGenContext()));
    return statement;
  }



  @Override
  protected String getMethodName() {
    return "addComplexUdtPropertyInfo";
  }
}
